package com.sample.base.ui.activity

import android.text.TextUtils
import android.view.MenuItem
import androidx.activity.result.ActivityResultLauncher
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.navigation.NavigationBarView
import com.loyal.adapter.ViewPager2Adapter
import com.loyal.helper.PermissionHelper
import com.sample.base.FileUtil
import com.sample.base.R
import com.sample.base.base.BaseActivity
import com.sample.base.databinding.ActivityPermissionBinding
import com.sample.base.ui.fragment.BlankFragment

class PermissionActivity : BaseActivity(), NavigationBarView.OnItemSelectedListener {
    private val binding by lazy { ActivityPermissionBinding.inflate(layoutInflater) }
    private var fragments: MutableList<Fragment> = ArrayList()
    private lateinit var singleLauncher: ActivityResultLauncher<String>

    override fun setLayoutOrView() {
        setContentView(binding.root)
    }

    override fun afterOnCreate() {
        binding.toolbar.toolbar.title = "权限申请"
        setSupportActionBar(binding.toolbar.toolbar)
        PermissionHelper.multiple(
            this, this,
            arrayOf(
                PermissionHelper.WRITE_EXTERNAL_STORAGE,
                PermissionHelper.READ_EXTERNAL_STORAGE, PermissionHelper.READ_PHONE_STATE,
                PermissionHelper.CAMERA
            )
        )
        singleLauncher = PermissionHelper.single(this, this)
        fragments.add(BlankFragment.newInstance("memory"))
        fragments.add(BlankFragment.newInstance("location"))
        fragments.add(BlankFragment.newInstance("phone"))
        fragments.add(BlankFragment.newInstance("camera"))
        binding.viewPager.adapter =
            ViewPager2Adapter(this, fragments)
        binding.navigation.setOnItemSelectedListener(this)
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> binding.navigation.selectedItemId = R.id.navigation_home
                    1 -> binding.navigation.selectedItemId = R.id.navigation_dashboard
                    2 -> binding.navigation.selectedItemId = R.id.navigation_notifications
                    3 -> binding.navigation.selectedItemId = R.id.navigation_bind
                }
            }
        })
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.navigation_home -> {
                binding.viewPager.currentItem = 0
                return true
            }
            R.id.navigation_dashboard -> {
                binding.viewPager.currentItem = 1
                return true
            }
            R.id.navigation_notifications -> {
                binding.viewPager.currentItem = 2
                return true
            }
            R.id.navigation_bind -> {
                binding.viewPager.currentItem = 3
                return true
            }
        }
        return false
    }

    override fun onFrag2Act(tag: String?, objTag: Any?) {
        super.onFrag2Act(tag, objTag)
        if (TextUtils.equals("camera", tag)) {
            singleLauncher.launch(PermissionHelper.CAMERA)
        }
    }

    override fun multiplePermissionsCallback(refuseMap: Map<String, Boolean>) {
        if (!refuseMap.contains(PermissionHelper.READ_EXTERNAL_STORAGE) &&
            !refuseMap.contains(PermissionHelper.WRITE_EXTERNAL_STORAGE)
        ) {
            FileUtil.createFileSys()
        }
    }

    /*fun onClick(view: View?) {
        if (!TextUtils.equals(ProgressBean.DOING, ProgressBean.NORMAL)) {
            ProgressBean.NORMAL = ProgressBean.DOING
            val intent = Intent()
            val apkUrl = "https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk"
            intent.putExtra("apkUrl", apkUrl)
            DownloadService.startAction(this, apkUrl)
        }
    }*/
}